

#include "loadingscene.h"
#include "resourcemanager.h"
#include "log.h"
#include "gamemanager.h"
#include "gamedatamanager.h"
#include "menuscene.h"
#include "CCSkeletonCache.h"

LoadingScene::LoadingScene(): _layer(nullptr) {

}

LoadingScene::~LoadingScene() {
}

bool LoadingScene::init() {
	if (!CCScene::init()) {
		return false;
	}

	_layer = CCLayer::create();
	addChild(_layer);

	CCSprite *spr = CCSprite::create("textures/loading.png");
	spr->setPosition(ccp(GM->screenSize.width / 2.0f, GM->screenSize.height / 2.0f));

/*
	spr->setAnchorPoint(CCPointZero);
	
	CCSize size = spr->getContentSize();
	spr->setScaleX(GM->screenSize.width / size.width);
	spr->setScaleY(GM->screenSize.height / size.height);*/
	_layer->addChild(spr);

	scheduleUpdate();

	return true;
}

void LoadingScene::load() {
	const char *arr[] = {
			"ui.plist",
			"equipments.plist",
			"equipments_icon.plist",
			"effects.plist",
	};

	GDM->init();
	RM->init();

	_DHLOG("load plist");

	for (int i = 0; i < (sizeof(arr) / sizeof(char *)); i++) {
		_DHLOG("load plist:%s", arr[i]);
		RM->loadTexturesFromPList(arr[i]);
	}

	_DHLOG("load skeleton");
	
	const char* jsons[] = {
							"1hand_weapon",
							"1hand_weapon_boar",
							"1hand_weapon_horse",
							"2hand_weapon",
							"2hand_weapon_boar",
							"2hand_weapon_horse",
							"ballista",
							"bow",
							"bow_boar",
							"bow_horse",
							"cannon",
							"catapult",
							"giant_flamethrower",
							"staff",
							"staff_boar",
							"staff_horse",
							"musket",
							"musket_boar",
							"musket_horse",
							"pistol",
							"pistol_boar",
							"pistol_horse",
							"plague_cannon",
							"spear",
							"spear_boar",
							"spear_horse",
							"steam_behemoth",
							"steam_chariot",
							"undead_catapult",

							"fx_hit_dark",
							"fx_hit_ice",
							"fx_hit_poison",
						};

	const char* jsonFile = "json/%s.json";
	char buf[200];
	for (int i = 0; i < sizeof(jsons) / sizeof(char *); i++) {
		sprintf(buf, jsonFile, jsons[i]);
		CCSkeletonCache::getInstance()->loadSkeleton(jsons[i], buf);
	}

	_DHLOG("all plist loaded.");
}

void LoadingScene::update(float dt) {
	load();

	CCDirector::sharedDirector()->replaceScene(MenuScene::create());
}


